package com.hunttown.mes.service;

import com.hunttown.mes.common.qcloud.qcloudsms.SmsSingleSend;
import com.hunttown.mes.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @title: 短信发送
 * @author: wangjunfu
 * @date: 2021年03月22日 14:48
 * @description:
 */
@Service
public class ASendSmsService {
    private final static Logger logger = LoggerFactory.getLogger(ASendSmsService.class);

    private final AnalysisConfigParamService configParamService;
    private final AHelpService aHelpService;
    private final TransportSmsTemplateService smsTemplateService;

    @Autowired
    public ASendSmsService(AnalysisConfigParamService configParamService, AHelpService aHelpService, TransportSmsTemplateService smsTemplateService) {
        this.configParamService = configParamService;
        this.aHelpService = aHelpService;
        this.smsTemplateService = smsTemplateService;
    }

    /**
     * 短信发送
     *
     * @param mobile   手机号
     * @param params   参数
     * @param templete 模板ID
     * @param smsSign  模板签名
     */
    public Map<String, String> sendSigleSms(String mobile, String[] params, Integer templete, String smsSign) {
        int entId = aHelpService.getEnterpriseId();
        return sendSigleSms(mobile, params, templete, smsSign, entId);
    }

    /**
     * 短信发送
     *
     * @param mobile   手机号
     * @param params   参数
     * @param templete 模板ID
     * @param smsSign  模板签名
     * @param entId    企业ID
     */
    public Map<String, String> sendSigleSms(String mobile, String[] params, Integer templete, String smsSign, Integer entId) {
        Map<String, String> map = new HashMap<>();

        //supplier 供应商：1腾讯云 2专用
        int supplier = configParamService.getByParamKey("sms-supplier", 2, entId);

        if (supplier == 1) {
            if (StringUtils.isBlank(smsSign)) {
                smsSign = smsTemplateService.getTempleteSignByID(templete + "", entId);
            }

            int appId = configParamService.getByParamKey("sms-tencent-appid", 0, entId);
            String appkey = configParamService.getByParamKey("sms-tencent-appkey", "", entId);
            map = SmsSingleSend.sendSmsByTemplete(appId, appkey, mobile, params, templete, smsSign);
        }

        if (supplier == 2) {
            //专用
        }

        return map;
    }
}
